package com.example.base.security.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.base.excetion.GlobalExcetion;
import com.example.base.security.SecurityUtil;
import com.example.base.security.SysUser;
import com.example.base.security.domain.SysMenuEntity;
import com.example.base.security.domain.SysRoleMenuEntity;
import com.example.base.security.domain.SysUserEntity;
import com.example.base.security.domain.vo.SysMenuVo;
import com.example.base.security.mapper.SysMenuMapper;
import com.example.base.security.service.SysMenuService;
import com.example.base.security.service.SysRoleMenuService;
import com.example.base.security.service.SysUserService;
import com.example.base.system.service.SysApiService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Xiongx
 * @version 1.0
 * @date 2021/6/11 14:48
 * @since JDK 1.8
 */
@Service("sysMenuService")
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenuEntity> implements SysMenuService {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysRoleMenuService sysRoleMenuService;

    @Autowired
    private SysMenuService sysMenuService;

    @Autowired
    private SysApiService sysApiService;

    @Override
    public List<SysMenuEntity> listTree() {
        List<SysMenuEntity> list=super.list();
        return getTree(list);
    }

    @Override
    public List<SysMenuEntity> listTreeByUser() {
        SysUser userEntity= SecurityUtil.getSysUserEntity();

        SysUserEntity entity=sysUserService.getById(userEntity.getUserId());
        if(entity.getRoleId()==null){
            return Collections.EMPTY_LIST;
        }
        List<SysRoleMenuEntity> list=null;
        if("admin".equals(userEntity.getUserName())){
            List<SysMenuEntity> sysMenuEntities=sysMenuService.listTree();
            return getTree(sysMenuEntities);
        }else {
            list =sysRoleMenuService.getSysRoleMenuEntityRid(entity.getRoleId());
            if(list==null|| list.size()==0){
                return Collections.EMPTY_LIST;
            }
            List<Integer> menuIds=list.stream().distinct().map(e->e.getMenuId()).collect(Collectors.toList());
            List<SysMenuEntity> sysMenuEntities=sysMenuService.listByIds(menuIds);
            return getTree(sysMenuEntities);
        }
    }

    @Override
    public List<SysMenuEntity> listParent() {
        LambdaQueryWrapper<SysMenuEntity> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysMenuEntity::getParentMenuId,0);
        return super.baseMapper.selectList(lambdaQueryWrapper);
    }

    @Override
    public SysMenuEntity saveOrUpdateSysMenuEntity(SysMenuVo vo) throws GlobalExcetion {
        if(vo.getParentMenuId()!=0){
            SysMenuEntity parent=super.getById(vo.getParentMenuId());
            if(parent==null){
                throw new GlobalExcetion(1,"父级菜单不存在");
            }
        }
        if(vo.getMenuId()==null){
            if(vo.getPermission()!=null){
                SysMenuEntity sysMenuEntity=getSysMenuEntityByPermission(vo.getPermission());
                if(sysMenuEntity!=null){
                    throw new GlobalExcetion(1,"菜单权限已存在");
                }
            }
            SysMenuEntity entity=new SysMenuEntity();
            BeanUtils.copyProperties(vo,entity);
            entity.setName(vo.getMenuName());
            super.baseMapper.insert(entity);
            sysApiService.dealSysApi(entity.getMenuId().toString(),vo.getApiList());
            return entity;
        }else {
            SysMenuEntity sysMenuEntity=super.getById(vo.getMenuId());
            if(sysMenuEntity==null){
                throw new GlobalExcetion(1,"菜单不存在，请刷新系统后重试");
            }
            if(vo.getPermission()!=null){
                if(!sysMenuEntity.getPermission().equals(vo.getPermission())){
                    SysMenuEntity permission=getSysMenuEntityByPermission(vo.getPermission());
                    if(permission!=null){
                        throw new GlobalExcetion(1,"菜单权限已存在");
                    }
                }
            }
            SysMenuEntity entity=new SysMenuEntity();
            BeanUtils.copyProperties(vo,entity);
            entity.setName(vo.getMenuName());
            super.baseMapper.updateById(entity);
            sysApiService.dealSysApi(entity.getMenuId().toString(),vo.getApiList());
            return entity;
        }

    }

    @Override
    public SysMenuEntity getSysMenuEntityByPermission(String permission) {
        LambdaQueryWrapper<SysMenuEntity> sysMenuEntityLambdaQueryWrapper=new
                LambdaQueryWrapper<>();
        sysMenuEntityLambdaQueryWrapper.eq(SysMenuEntity::getPermission,permission);
        return super.baseMapper.selectOne(sysMenuEntityLambdaQueryWrapper);
    }

    private static List<SysMenuEntity> getTree(List<SysMenuEntity> list){
        List<SysMenuEntity> array=new ArrayList<>();
        for (SysMenuEntity sysMenuEntity : list) {
            if(sysMenuEntity.getParentMenuId()!=null && sysMenuEntity.getParentMenuId()==0){
                getTreeChild(sysMenuEntity,list);
                array.add(sysMenuEntity);
            }
        }
        return array;
    }

    private static void getTreeChild(SysMenuEntity entity,List<SysMenuEntity> list){
        for (SysMenuEntity sysMenuEntity : list){
            if(sysMenuEntity.getParentMenuId() !=null && entity.getMenuId().equals(sysMenuEntity.getParentMenuId())){
                if(entity.getChild()==null){
                    entity.setChild(new ArrayList<>());
                }
                entity.getChild().add(sysMenuEntity);
                getTreeChild(sysMenuEntity,list);
            }
        }
    }

}
